package ServiceTest

import impl.ISomeServiceImpl
import org.junit.Test
import org.springframework.context.support.ClassPathXmlApplicationContext
import org.springframework.context.support.FileSystemXmlApplicationContext

/**
 * Created by futao on 2017/9/29.
 */
class myTest4Kotlin {
    /**
     * 传统方式 new对象
     */
    @Test
    fun test() {
        val iSomeServiceImpl = ISomeServiceImpl()
        iSomeServiceImpl.doFirst()
        iSomeServiceImpl.doSecond()
    }

    /**
     * 从bean容器里面获取
     */
    @Test
    fun test02() {
        //加载Spring配置文件，创建Spring对象，这个时候对象就已经全部被加载到Spring容器当中了
//        val container = ClassPathXmlApplicationContext("applicationContext.xml")
        val container = FileSystemXmlApplicationContext("D:\\eclipse-workspace\\spring4\\src\\main\\resources\\applicationContext.xml")
        //从容器中获取指定Bean对象
        val iss = container.getBean("iss") as ISomeServiceImpl
        iss.doSecond()
        iss.doFirst()
    }

    @Test
    fun test03() {
        //销毁方法的执行需要两个要求
        /*
        *   1)被销毁的对象需要是singleton的
        *   2)容器要显式地关闭
        * */
        val iss = ClassPathXmlApplicationContext("applicationContext.xml")
        val service = iss.getBean("iss") as ISomeServiceImpl
        println(service.doFirst())
        service.doSecond()
        println("================")
        val s2 = iss.getBean("dyb") as ISomeServiceImpl
        s2.doSecond()
        s2.doFirst()
        iss.destroy()
    }

    @Test
    fun forTest() {
        val iss = ClassPathXmlApplicationContext("applicationContext.xml")
        val obj = iss.getBean("iss") as ISomeServiceImpl
        iss.destroy()
    }

    //    @Test
//    fun testFactory(){
//        val beanFactory=XmlBeanFactory().apply {
//            ClassPathXmlApplicationContext("applicationContext.xml")
//        }
//
//    }
    @Test
    fun myTest() {
        val iss = ClassPathXmlApplicationContext("applicationContext.xml")
        val obj = iss.getBean("iss") as ISomeServiceImpl
        obj.doFirst()
//        iss.destroy()
        iss.close()
    }

    @Test
    fun myTest01() {

    }
}



